#include "cabecera.h"

void* generaVector(int elementos, int tipo){
	void* vector;
	switch(tipo){
		case 1:
		vector = (int*)malloc(elementos*sizeof(int));
		break;
	
		case 2:
		vector = (long int*) malloc(elementos*sizeof(long int));
		break;

		case 3:
		vector = (double*) malloc(elementos*sizeof(double));
		break;

		case 4:
		vector = (float*) malloc(elementos*sizeof(float));
		break;
	}

	return vector;
}

void rellenaVector(void* vector, int tipo, int elementos){
	int i;
	for(i=0; i<elementos; i++){
		printf("Introduzca el valor %d: ", i);
		if(tipo == 1){
			scanf("%d", (int*) vector+i);
		}
		else if(tipo == 2){
			scanf("%ld", (long int*) vector+i);
		}
		else if(tipo == 3){
			scanf("%lf", (double*) vector+i);
		}
		else if(tipo == 4){
			scanf("%f", (float*) vector+i);
		}
	}
}

void imprimeVector(void* vector, int tipo, int elementos){
	int i;
	for(i=0; i<elementos ; i++){
		if(tipo == 1){
			printf("%d -", *( (int*) vector+i));
		}
		else if(tipo == 2){
			printf("%ld -", *( (long int*) vector+i));
		}
		else if(tipo == 3){
			printf("%lf -", *( (double*) vector+i));
		}
		else if(tipo == 4){
			printf("%f -", *( (float*) vector+i));
		}
	}
	printf("\n");
}

void mayorElemento(void* vector, int tipo, int nElementos, void** max){
	int i;
	void *mayor, *aux;
	switch(tipo){
		case 1:
			mayor = (int*) vector;
			for(i=0; i<nElementos; i++){
				aux = (int*) vector+i;
				if(*( (int*) aux) > *( (int*) mayor)){
					mayor = (int*) aux;
				}
			}
		break;

		case 2:
			mayor = (long int*) vector;
			for(i=0; i<nElementos; i++){
				aux = (long int*) vector+i;
				if(*( (long int*) aux) > *( (long int*) mayor)){
					mayor = (long int*) aux;
				}
			}	
		break;

		case 3:
			mayor = (double*) vector;
			for(i=0; i<nElementos; i++){
				aux = (double*) vector+i;
				if(*( (double*) aux) > *( (double*) mayor)){
					mayor = (double*) aux;
				}
			}
		break;

		case 4:
			mayor = (float*) vector;
			for(i=0; i<nElementos; i++){
				aux = (float*) vector+i;
				if(*( (float*) aux) > *( (float*) mayor)){
					mayor = (float*) aux;
				}
			}
		break;
	}

	*max = mayor;
}
